package keter.combofs.path.impl;

import keter.combofs.path.PathMaker;

import java.io.File;

/**
 * 文件系统子目录生成默认规则
 * Created by gulx@neusoft.com on 2019/11/11.
 */
public class PathMakerFSImpl implements PathMaker {

    /**
     * 根据文件名称的哈希值取0~256的一级子目录和0~16的二级子目录
     * @param fileName
     * @return
     */
    @Override
    public String makePath(String fileName) {
        int lv1 = fileName.hashCode() & 0xff;
        String lv1Str = String.valueOf(fileName.hashCode() & 0xff);
        lv1Str = complete(lv1Str);
        String lv2 = String.valueOf(lv1 & 0xf);
        lv2 = complete(lv2);
        return File.separator + lv1Str + File.separator + lv2 + File.separator ;
    }

    @Override
    public String getPathById(Long id) {
        // 文件系统模式下可以根据ID+算法直接定位文件存放目录
        return makePath(id.toString());
    }

    /**
     * 补齐3位长度
     * @param path
     * @return
     */
    private String complete(String path) {
        if(path.length()<=1)
             path="00"+path;
        if(path.length()<=2)
             path="0"+path;
        return path;
    }

}
